package reducejoin;

import org.apache.hadoop.io.LongWritable;
import org.apache.hadoop.io.Text;
import org.apache.hadoop.mapreduce.Mapper;
import org.apache.hadoop.mapreduce.lib.input.FileSplit;

import java.io.IOException;

/**
 * LongWritable,Text 偏移量,数据文本
 * Text,Bean       pid,Bean
 */
public class TableMapper extends Mapper<LongWritable, Text, Text, TableBean> {
    Text k = new Text();
    TableBean bean = new TableBean();

    @Override
    protected void map(LongWritable key,
                       Text value,
                       Context context) throws IOException, InterruptedException {
        //通过context找到InputSplit的子类FileSplit 注:转类型
        FileSplit split = (FileSplit) context.getInputSplit();
        //找到文件名：用于判断两个文件
        String name = split.getPath().getName();

        String line = value.toString();

        //按0订单表处理：orderId,pid,amount
        if(name.startsWith("order")){
            String[] fields = line.split("\t");
            bean.setOrderId(fields[0]);
            bean.setPid(fields[1]);
            bean.setAmount(Integer.parseInt(fields[2]));
            bean.setPname("");
            bean.setFlag("0");

            k.set(fields[1]);
        }else {
            //按1 产品表处理
            String[] fields = line.split("\t");
            bean.setPid(fields[0]);
            bean.setPname(fields[1]);
            bean.setAmount(0);
            bean.setOrderId("");
            bean.setFlag("1");

            k.set(fields[0]);
        }
        context.write(k,bean);

    }
}
